package com.leyou.user.interceptor;

import com.leyou.common.auth.pojo.AppInfo;
import com.leyou.common.auth.pojo.Payload;
import com.leyou.common.auth.utils.JwtUtils;
import com.leyou.common.constants.LyConstants;
import com.leyou.user.config.JwtProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@Component
public class AuthInterceptor implements HandlerInterceptor {

    @Autowired
    private JwtProperties jwtProp;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //获取token
//        String token = request.getHeader(LyConstants.APP_TOKEN_HEADER);
//        //判空
//        if(StringUtils.isEmpty(token)){
//            log.error("【服务之间鉴权】失败！来访服务没有携带服务token！");
//            //阻止访问处理器
//            return false;
//        }
//        //解析token
//        Payload<AppInfo> payload = null;
//        try {
//            payload = JwtUtils.getInfoFromToken(token, jwtProp.getPublicKey(), AppInfo.class);
//        }catch (Exception e){
//            log.error("【服务之间鉴权】失败！来访服务携带的token不合法！");
//            //阻止访问处理器
//            return false;
//        }
//        //获取来访服务可以访问的服务列表
//        List<Long> targetList = payload.getUserInfo().getTargetList();
//        //判断来访服务是否有当前服务的访问权限
//        if(!targetList.contains(jwtProp.getApp().getId())){
//            log.error("【服务之间鉴权】失败！来访服务没有当前服务的访问权限！");
//            //阻止访问处理器
//            return false;
//        }
        return true;
    }

}
